package site.wuao.simplerxjava.base;

import android.util.Log;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * 订阅（被观察者 订阅 观察者）
 *
 * @author wuao
 * @date 2017/11/29
 * @github {https://github.com/CoderWuao}
 * @blogs {http://wuao.site/}
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @github -
 * @blogs -
 * @note -
 */
public class RxSubscribe {
    /** 标记 */
    private static final String TAG = "RxSubscribe";

    /**
     * 订阅 - just
     */
    public void subscribeByJust() {
        Observable
                .just(1, 2, 3)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i(TAG, "onStart");
                    }
                });

        // onStart
        // onNext: 1
        // onNext: 2
        // onNext: 3
        // onCompleted
    }

    /**
     * 订阅 - create
     */
    public void subscribeByCreate() {
        Observable
                .create(new Observable.OnSubscribe<Integer>() {

                    @Override
                    public void call(Subscriber<? super Integer> subscriber) {
                        Log.i(TAG, "call_01");
                        subscriber.onNext(1);
                        Log.i(TAG, "call_02");
                        subscriber.onCompleted();
                        Log.i(TAG, "call_02");
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError：" + e.getMessage());
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i(TAG, "onStart");
                    }
                });

        // onStart
        // call_01
        // onNext: 1
        // call_02
        // onCompleted
        // call_02
    }

    /**
     * 订阅 - 不完整回调
     */
    public void subscribeByIncompleteCallback() {
        Observable
                .just(1, 2, 3)
                .subscribe(
                        new Action1<Integer>() {
                            @Override
                            public void call(Integer integer) {
                                Log.i(TAG, "onNext: " + integer);
                            }
                        },
                        new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                Log.i(TAG, "onError");
                            }
                        },
                        new Action0() {
                            @Override
                            public void call() {
                                Log.i(TAG, "onCompleted");
                            }
                        }
                );

        // onNext: 1
        // onNext: 2
        // onNext: 3
        // onCompleted
    }
}
